<?php
class MsgHelper {
	public static function sendSMS($phone, $message)
	{
		//uid : GZLKJ003200
		//pwd: lc@668
		$uid = Config::get('interface.sms.gateway_uid');
		$pwd = Config::get('interface.sms.gateway_pwd');
		$message = str_replace("'", '', $message).'【我的律团网】';
		$message = iconv('UTF-8', 'GB2312//IGNORE', $message);
		$gateway = "http://sdk.zhongguowuxian.com:98/ws/batchSend.aspx?CorpID={$uid}&Pwd={$pwd}&Mobile={$phone}&Content=".urlencode($message)."&Cell=&SendTime=";
		//exit($gateway);
		$result = file_get_contents($gateway);
		if ($result==0 || $result ==1 ){
			return true;
		}
		return false;
	}
	
	/**
	 * 向律师推送问律师消息
	 * @param int $lawyer_openid 律师openid
	 * @param int $question_id 问律师帖子id
	 * @param string $title 帖子标题
	 * @param string $ask_name 提问者名字
	 * @param string $content 提问内容
	 */
	public static function sendQuestionToLawyer($lawyer_openid, $question_type, $question_id, $title, $ask_name, $content)
	{
		if (!$ask_name) {
            $ask_name = "匿名";
        }
		$template_id = Config::get('interface.weixin.QUESTION_TEMPLATE_ID');
		$content =  mb_substr($content, 0, 100, 'utf-8');
		$data= array(
			'first' => array('value'=>'您有一条用户咨询待解决', 'color'=>'#000'),
			'keyword1' => array('value'=>date("Y-m-d H:i:s"), 'color'=>'#000'),
			'keyword2' => array('value'=>$ask_name, 'color'=>'#000'),
			'keyword3' => array('value'=>$title, 'color'=>'#000'),
			'remark' => array('value'=>"咨询内容：" . $content . "\n\n点击处理咨询",'color'=>'#333'),
		);

		$url = Config::get('app.wechat_web_url') . sprintf("/#/questionsview/%s", $question_id);
		$wx = new WeixinHelper();
		$result = $wx->sendMessage($lawyer_openid, $template_id, $url, $data);
		return $result;
		
	}
	
	/**
	 * 发送回复提醒给题主
	 * @param string $creator_openid po主微信id
	 * @param string $commenter_name 回复者姓名
	 * @param string $content 回复内容
	 * @param Post $post
	 */
	public static function sendReplyToPostCreator($creator_openid, $commenter_name, $content, $post)
	{
		$template_id = Config::get('interface.weixin.REPLY_TEMPLATE_ID');
		$content =  mb_substr($content, 0, 100, 'utf-8');
		$data= array(
			'first' => array('value'=>sprintf("咨询主题：%s", $post->title), 'color'=>'#000'),
			'keyword1' => array('value'=>$commenter_name, 'color'=>'#000'),
			'keyword2' => array('value'=>date("Y-m-d H:i:s"), 'color'=>'#000'),
			'keyword3' => array('value'=>$content, 'color'=>'#000'),
			'remark'	=> array('value'=>"\n点击进行回复",'color'=>'#333'),
		);
		 
		$url = Config::get('app.wechat_web_url') . sprintf("/#/easemobmain/%s", $post->id);
		$wx = new WeixinHelper();
		$result = $wx->sendMessage($creator_openid, $template_id, $url, $data);
	}
}
